package com.wt.springsamples.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

public class NIOClient {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        InetSocketAddress serverInetSocketAddress = new InetSocketAddress("127.0.0.1",8000);
        if(!socketChannel.connect(serverInetSocketAddress)){
            //设置了非阻塞模式，socketChannel.configureBlocking(false);
            while (!socketChannel.finishConnect()){
                System.out.println("异步连接服务端。。。。");
            }
        }
        Scanner scanner = new Scanner(System.in);// 创建输入流扫描器
        //发送数据
        while (scanner.hasNextLine()){
            String line = scanner.nextLine();// 获取用户输入的一行文本
            ByteBuffer buffer = ByteBuffer.wrap(line.getBytes());
            socketChannel.write(buffer);
        }
    }
}
